<?php
require_once './component/head.php';
if (!isset($_SESSION['uid'])) header('Location:./login.php');
require_once './component/drawer.php';
require_once './function/conn.php';
//表格颜色设置
$i = 0;
?>
    <script type="text/javascript">
        window.onload = function () {
            CourseOnload();
        };
    </script>

    <div id="course_page" class="mdui-container mdui-center drxs-page">
        <div id="course_management">
            <div class="mdui-table-fluid drxs-radius">
                <table id="all_course_table" class="mdui-table mdui-table-hoverable">
                    <thead>
                    <tr class="<?php echo $_SESSION['table_color_1'] ?>">
                        <th>课程id</th>
                        <th>课程名称</th>
                        <th>学时</th>
                        <th>学分</th>
                        <th>授课教师</th>
                        <th>教室</th>
                        <th>上课时间</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    $all_course = QueryCourse();
                    while ($course_info = mysqli_fetch_array($all_course)) :
                        if (($i++) % 2 == 0)
                            echo '<tr class=\'' . $_SESSION['table_color_2'] . '\'>';
                        else
                            echo '<tr class=\'' . $_SESSION['table_color_3'] . '\'>';
                        ?>
                        <td><?php echo $course_info['CourseId'] ?></td>
                        <td><?php echo $course_info['CourseName'] ?></td>
                        <td><?php echo $course_info['Hour'] ?></td>
                        <td><?php echo $course_info['Credit'] ?> </td>
                        <td><?php echo $course_info['TeacherName'] ?></td>
                        <td><?php echo $course_info['ClassroomName'] ?></td>
                        <td><?php echo $course_info['Time'] ?></td>
                        </tr>
                    <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
            <?php if ($_SESSION['utype'] == '管理员' || $_SESSION['utype'] == '教师') { ?>
                <button id="btn_add_course_btn" class="mdui-btn mdui-btn-raised mdui-ripple mdui-color-theme-accent">
                    <a class="mdui-text-color-white">添加新课程</a>
                </button>
            <?php } ?>
        </div>
        <?php
        if ($_SESSION['utype'] == '管理员' || $_SESSION['utype'] == '教师') {
            $teacher_info = QueryDb("select * from drxs_user where Type='教师'");
            $teacher_num = mysqli_num_rows($teacher_info);
            $classroom_info = QueryDb("select * from drxs_classroom");
            $classroom_num = mysqli_num_rows($classroom_info);
            ?>
            <div id="add_course">
                <div class="mdui-card mdui-center">
                    <div class="mdui-card-content">
                        <div class="mdui-typo"><h5>请输入以下基本信息，以添加新课程。</h5></div>
                        <div class="mdui-divider"></div>
                        <form method="post" action="./function/function.php">
                            <div class="mdui-textfield mdui-textfield-floating-label">
                                <label class="mdui-textfield-label">课程名称</label>
                                <input class="mdui-textfield-input" name="add_course_name" type="text" required/>
                            </div>
                            <div class="mdui-row" style="padding-top: 14px">
                                <div class="mdui-col-xs-6">
                                    <label class="mdui-textfield-label">请选择教师（共<?php echo $teacher_num; ?>个）</label>
                                    <select class="mdui-select" size="3" name="add_course_teacher"
                                            mdui-select="{position: 'bottom'}">
                                        <option value="none" selected disabled>请选择教师</option>
                                        <?php
                                        while ($teacher = mysqli_fetch_array($teacher_info))
                                            echo '<option value=' . $teacher['Id'] . '> ' . $teacher['Id'] . ' - ' . $teacher['Name'] . '</option>';
                                        ?>
                                    </select>
                                </div>
                                <div class="mdui-col-xs-6">
                                    <label class="mdui-textfield-label">请选择教室（共<?php echo $classroom_num; ?>个）</label>
                                    <select class="mdui-select" size="3" name="add_course_classroom"
                                            mdui-select="{position: 'bottom'}">
                                        <option value="none" selected disabled>请选择教室</option>
                                        <?php
                                        while ($classroom = mysqli_fetch_array($classroom_info))
                                            echo '<option value=' . $classroom['ClassroomId'] . '> ' . $classroom['ClassroomName'] . ' - ' . $classroom['Note'] . '</option>';
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="mdui-row">
                                <div class="mdui-col-xs-6">
                                    <div class="mdui-textfield mdui-textfield-floating-label">
                                        <label class="mdui-textfield-label" id="credit_val">学分</label>
                                        <input class="mdui-textfield-input" name="add_course_credit" value="1"
                                               type="number" required/>
                                    </div>
                                </div>
                                <div class="mdui-col-xs-6">
                                    <div class="mdui-textfield">
                                        <label class="mdui-textfield-label">学时</label>
                                        <input class="mdui-textfield-input" name="add_course_hour" type="number"
                                               value="16" required readonly/>
                                    </div>
                                </div>
                            </div>
                            <div class="mdui-textfield mdui-textfield-floating-label">
                                <label class="mdui-textfield-label">上课时间</label>
                                <input class="mdui-textfield-input" name="add_course_time" type="text" required/>
                            </div>
                            <div class="mdui-card-actions">
                                <button type="button"
                                        class="mdui-btn mdui-btn-raised mdui-ripple mdui-color-theme-accent mdui-float-right"
                                        id="add_course_btn"><a class="mdui-text-color-white">提交信息</a>
                                </button>
                                <button class="mdui-btn mdui-btn-raised mdui-ripple mdui-color-theme-accent mdui-float-right"
                                        type="reset"
                                        mdui-tooltip="{content: '将清除已填写的数据'}"><a class="mdui-text-color-white">重新填写</a>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        <?php } ?>
        <?php if ($_SESSION['utype'] == '教师') { ?>
        <div id='teaching_course'>
            <div class="mdui-table-fluid drxs-radius">
                <table id="all_course_table" class="mdui-table mdui-table-hoverable">
                    <thead>
                    <tr class="{$_SESSION['table_color_1']}">
                        <th>课程id</th>
                        <th>课程名称</th>
                        <th>学时</th>
                        <th>学分</th>
                        <th>教室</th>
                        <th>上课时间</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    $all_teaching = QueryTeachingCourse($_SESSION['uid']);
                    if (mysqli_num_rows($all_teaching) != 0) {
                        while ($teaching_info = mysqli_fetch_array($all_teaching)) {
                            if (($i++) % 2 == 0)
                                echo '<tr class=\'' . $_SESSION['table_color_2'] . '\'>';
                            else
                                echo '<tr class=\'' . $_SESSION['table_color_3'] . '\'>';
                            ?>
                            <td><?php echo $teaching_info['CourseId']; ?></td>
                            <td><?php echo $teaching_info['CourseName']; ?></td>
                            <td><?php echo $teaching_info['Hour']; ?></td>
                            <td><?php echo $teaching_info['Credit']; ?></td>
                            <td><?php echo $teaching_info['ClassroomName']; ?></td>
                            <td><?php echo $teaching_info['Time']; ?></td>
                            </tr>
                        <?php }
                    } else {
                        echo '<td colspan="6">当前暂无数据，请稍后再试！</td>';
                    }
                    } ?>
                    </tbody>
                </table>
            </div>

<?php require_once './component/footer.php'; ?>